﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;

public class Camp : NetworkBehaviour
{
    public float MaxHp;

    [SyncVar(hook = "ChangHP")]
    public float currentHP;
    private float timer = 0;
    public GameObject cam;

    public bool chargeLocal = false;
    public bool chargeServer = false;

    public GameObject Grids;
    public void Awake()
    {
        Grids = (GameObject)Resources.Load("Prefabs/Grids");//建筑模型
    }

    public override void OnStartLocalPlayer()
    {
        //本端情况，建筑tag为1
        transform.name = "Camp1";
        transform.tag = "Camp1";
        this.gameObject.layer = 11;
        foreach (Transform castlePart in transform)
        {
            if (!castlePart.gameObject.GetComponent<Camera>())
                castlePart.gameObject.tag = "Player 1 castle";
        }
        cam.SetActive(true);
    }

    public void Start()
    {
        if (transform.position.x < 0)
        {
            transform.localEulerAngles = new Vector3(0, 180, 0);
        }
        else if (transform.position.x > 0)
        {
            transform.localEulerAngles = new Vector3(0, 0, 0);
        }
        chargeLocal = isLocalPlayer;
        chargeServer = isServer;
        MaxHp = 0;
        for (int i = 0; i < this.transform.childCount - 1; i++)
        {
            MaxHp += this.transform.GetChild(i).GetComponent<Castle>().lives;
        }
        currentHP = MaxHp;
        GameObject[] flags = GameObject.FindGameObjectsWithTag("Flag");
        foreach (GameObject flag in flags)
        {
            if (flag.transform.position.x < 0)
            {
                flag.GetComponent<SkinnedMeshRenderer>().material.color = Color.red;
            }
            else
                flag.GetComponent<SkinnedMeshRenderer>().material.color = Color.blue;
        }
        if (!isLocalPlayer)
        {
            transform.name = "Camp2";
            transform.tag = "Camp2";
            this.gameObject.layer = 12;
            foreach (Transform castlePart in transform)
            {
                if (!castlePart.gameObject.GetComponent<Camera>())
                {
                    castlePart.gameObject.tag = "Player 2 castle";
                }
            }
            return;
        }
        if (transform.position.x < 0)
        {
            Grids.transform.localEulerAngles = new Vector3(0, 0, 0);
            Grids.transform.position = new Vector3(this.transform.position.x + 14, 13.5f, 0);
        }
        else if (transform.position.x > 0)
        {
            Grids.transform.localEulerAngles = new Vector3(0, 180, 0);
            Grids.transform.position = new Vector3(this.transform.position.x - 14, 13.5f, 0);
        }
        timer = 0;
    }
    public void ChangHP(float newHP)
    {
        currentHP = newHP;
    }
    void Update()
    {
        if (timer >= 1)
        {
            GameJudge.instance.Player1.GetComponent<Resource>().AddCoin(5);
            timer = 0;
        }
        timer += Time.deltaTime;
        //本端逻辑代码，对方端不执行之后代码
        if (!isServer)
            return;
        float temp = 0;
        for (int i = 0; i < this.transform.childCount - 1; i++)
        {
            temp += this.transform.GetChild(i).GetComponent<Castle>().lives;
        }
        currentHP = temp;
    }
}
